﻿



using HBase;
using hCommand;
using Net.http;
using Persistence;
using Persistence.mySql;
using Persistence.sqlserver;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;

namespace WebApi {
    [FormAuthenticationFilter]

    [RoutePrefix("CoreStockDet")]
    public class CoreStockDetController : BaseApiController {
        ICoreStockServer coreStockServer = WMSDalFactray.getDal<ICoreStockServer>();
        IBatchServer batchServer = WMSDalFactray.getDal<IBatchServer>();
        ICSDServer csdServer = WMSDalFactray.getDal<ICSDServer>();


        [HttpPost, Route("findDetailPage")]
        public BaseResult findDetailPage([FromBody] CoreStockDetalDto dto) {
            Page<CoreStockDet> info = csdServer.QueryCoreStockDetailPage(dto);
            var infocontent = info.content.OrderBy(x => x.status);
            return BaseResult.Ok(info);
        }

        [HttpPost, Route("printDetail")]
        public BaseResult printDetail([FromBody] CoreStockDetalDto dto) {
            PrintDetail pd = new PrintDetail();
            pd.info = csdServer.listCoreStockDetail(dto);
            pd.html = getPrintUtf8("stockDetail.html");

            return BaseResult.Ok(pd);
        }
        public static object locler = new object();
        public static bool saving = false;

        [HttpPost, Route("SaveStock"), ControlName("托盘保存")]
        public BaseResult SaveStock(StockDto model) {
            if (saving) {
                return BaseResult.Error("保存堵塞请重试");
            }
            lock (locler) {
                saving = true;
                try {
                    CoreStock stock = model.main;
                    if (stock == null) {
                        return BaseResult.Error("托盘信息异常");
                    }
                    if (string.IsNullOrWhiteSpace(stock.boxCode)) {
                        return BaseResult.Error("托盘条码不可为空");
                    }
                    CoreStock cs = coreStockServer.getCoreStockByCode(stock.boxCode);
                    if (cs != null) {
                        return BaseResult.Error("条码已经存在");
                    }

                    if (stock.id == 0) {
                        stock.status = (int)StockStatus.库外;
                        stock.changeTime = DateTime.Now;
                    }
                    coreStockServer.saveOne2Many(model);
                    return BaseResult.Ok(stock);
                } finally {
                    saving = false;
                }
            }
        }


        [HttpPost, Route("pdaHideDet"), ControlName("pad隐藏")]
        public BaseResult pdaHideDet(CoreStockDet model) {
            var det = csdServer.getById<CoreStockDet>(model.id);
            det.status = model.status;
            csdServer.updateNotNull(det);
            return BaseResult.Ok("ok");
        }

        [HttpPost, Route("saveStockDet"), ControlName("组盘保存")]
        public BaseResult saveStockDet([FromBody] CoreStockDet dto) {//手动调整 组盘
            if (dto.countDb == 0) {
                return BaseResult.Ok("资产数量不能为0");
            }
            TransRes res = WMSTransactionFacade.doChangeCSDEngine(dto);
            if (!res.s) {
                return BaseResult.Error(res.msg);
            }
            return BaseResult.Ok("保存成功");
        }
        
        [HttpPost, Route("GetListByStockId")]
        public BaseResult GetListByStockId(CoreStock model) {
            return BaseResult.Ok(csdServer.GetListByStockId(model.id));
        }
        [HttpPost, Route("detOut"), ControlName("明细主键出库")]
        public BaseResult detOut(CoreStockDet dto) {
            TransRes res = WMSTransactionFacade.doOutDetEngine(dto.id);
            if (res.s == true) {
                return BaseResult.Ok(dto.barCode + "出库成功");
            } else {
                return BaseResult.Error(res.msg);
            }
        }

        [HttpPost, Route("selectOut"), ControlName("明细主键出库查询")]
        public BaseResult selectDetOut(CoreStockDet dto) {

            TransRes res = WMSTransactionFacade.doOutDetEngine(dto.id);
            if (res.s == true) {
                return BaseResult.Ok(dto.barCode + "出库成功");
            } else {
                return BaseResult.Error(res.msg);
            }
        }

        [HttpPost, Route("deleteStockDetail"), ControlName("删除托盘明细库存")]
        public BaseResult deleteStockDetail(CoreStockDet model) {

            var r = WMSTransactionFacade.doOutDetEngine(model.id);
            if (r.s) {
                return BaseResult.Ok(r.msg);
            } else {
                return BaseResult.Error(r.msg);
            }
        }
        [HttpPost, Route("stockDetailOut"), ControlName("设备出库")]
        public BaseResult stockDetailOut(CoreStockDetalDto dto) {
            List<CoreStockDet> list = csdServer.getDetail(new List<String>() { dto.barCode });
            foreach (CoreStockDet csd in list) {
                WMSTransactionFacade.doOutDetEngine(csd.id);
            }
            return BaseResult.Error(dto.barCode + "出库成功");
        }

      

        [HttpPost, Route("getStockDetail"), ControlName("查看明细")]
        public BaseResult getStockDetail(CoreStockDet model) {
            if (model == null || model.id == 0) {
                return BaseResult.Ok(null);
            }
            var v = coreStockServer.getById<CoreStockDet>(model.id);
            return BaseResult.Ok(v);
        }
        [HttpPost, Route("setDetailStock"), ControlName("明细更换托盘")]
        public BaseResult setDetailStock(CoreStockDet model) {
            var v = WMSTransactionFacade.doCsd2CsEngine(model.id,model.pid);
            return BaseResult.Ok(v);
        }
        [HttpPost, Route("changeDetBox"), ControlName("修改明细托盘")]
        public BaseResult changeDetBox([FromBody] CoreStockDet p) {

            CoreStock cs = coreStockServer.getCoreStockByCode(p.pCode);
            if (cs == null) {
                return BaseResult.Error(p.pCode + "托盘不存在");
            }
            TransRes res = WMSTransactionFacade.doCsd2CsEngine(p.id, cs.id);
            return BaseResult.Ok(res.msg);
        }






        [HttpPost, Route("detOutEngine"), ControlName("明细出库")]
        public BaseResult detOutEngine(CoreStockDet det) {
            TransRes res = WMSTransactionFacade.doDetOutEngine(det.id);
            if (res.s) {
                return BaseResult.Ok("生成成功");
            } else {
                return BaseResult.Error(res.msg);
            }
        }


        [HttpPost, Route("splitDetEngine"), ControlName("拆分明细")]
        public BaseResult splitDetEngine(CoreStockDet det) {
            TransRes res = WMSTransactionFacade.doSplitDetEngine(det);
            if (res.s) {
                return BaseResult.Ok("成功");
            } else {
                return BaseResult.Error(res.msg);
            }
        }

      
        [HttpPost, Route("changeCsdDooDetId/{pkId}/{add}/{del}"), ControlName("修改明细出库单")]
        public BaseResult changeCsdDooDetId(long pkId, String add, String del) {
            csdServer.changeCsdDooDetId(pkId, add, del);
            return BaseResult.Ok("删除成功");
        }
        [HttpPost, Route("UpdatePriorityByCoreId"), ControlName("修改批次优先级")]
        public BaseResult UpdatePriorityByCoreId([FromBody] CoreStockDto model) {
            bool result2 = csdServer.UpdatePriorityById(model);
            WhBatch whModel = new WhBatch();
            whModel.id = (long)model.batchId;
            whModel.priority = model.priority;

            bool result = batchServer.UpdatePriority(whModel);
            return BaseResult.Ok(result);

        }
        [HttpPost, Route("changeDetailPos"), ControlName("更改位置")]
        public BaseResult changeDetailPos(CoreStockDetalDto dto) {
            List<CoreStockDet> list = csdServer.getDetail(dto.stockId, false);

            for (int i = 0; i < list.Count; i++) {
                CoreStockDet csd = list[i];
                if (csd.id == dto.id) {
                    if (dto.dir == 1) {
                        if (list.Count > i + 1) {
                            list[i] = list[i + 1];
                            list[i + 1] = csd;
                        }

                    } else if (dto.dir == -1) {
                        if (i > 0) {
                            list[i] = list[i - 1];
                            list[i - 1] = csd;
                        }
                    }
                }
            }
            for (int i = 0; i < list.Count; i++) {
                CoreStockDet csd = list[i];
                if (csd.bPos != i + 1) {
                    csd.bPos = i + 1;
                    csdServer.updateNotNull(csd);
                }
            }
            return BaseResult.Error(dto.barCode + "不可出库");
        }

    }
}